package com.ruoyi.provider.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.exceptions.base.BeanValidatorsOne;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.provider.domain.AhctCase;
import com.ruoyi.provider.domain.AhctCaseCode;
import com.ruoyi.provider.mapper.AhctInformationMapper;
import com.ruoyi.provider.service.AhctInformationService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.List;


/**
 * @author Wang RuLong
 * @description 针对表【AhctCase(案件信息表)】的数据库操作Service实现
 * @createDate 2024-05-17 10:58:33
 */

@Service
public class AhctInformationServiceImpl implements AhctInformationService {

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
    @Resource
    private AhctInformationMapper ahctInformationMapper;

    @Resource
    protected Validator validator;


    //获取案件管理列表
    @Override
    public PageInfo<AhctCase> getCaseCondition(AhctCase ahctCase,int pageNum,int pageSize) {
        // 开启分页
        PageHelper.startPage(pageNum,pageSize);
        // 执行查询
        List<AhctCase> surveyList = ahctInformationMapper.selectSurveyGuery(ahctCase);
        // 封装为PageInfo
        PageInfo<AhctCase> pageInfo;

        pageInfo = new PageInfo<>(surveyList);
        return pageInfo;
    }


    //案件管理信息录入
    @Override
    public void insertCase(AhctCaseCode ahctCaseCode) {

        List<String> list = ahctCaseCode.getAddress_code();
        String addResCode = String.join(",",list);
        AhctCase ahctCase = new AhctCase();
        ahctCase.setAddress_code(addResCode);
        ahctCase.setCar_number(ahctCaseCode.getCar_number());
        ahctCase.setArea(ahctCaseCode.getArea());
        ahctCase.setCity(ahctCaseCode.getCity());
        ahctCase.setAccident_address(ahctCaseCode.getAccident_address());
        ahctCase.setCompany_name(ahctCaseCode.getCompany_name());
        ahctCase.setProvince(ahctCaseCode.getProvince());
        ahctCase.setReceiving_time(ahctCaseCode.getReceiving_time());
        ahctCase.setSurveyer(ahctCaseCode.getSurveyer());
        ahctCase.setCase_number(ahctCaseCode.getCase_number());
        ahctCase.setCase_type(ahctCaseCode.getCase_type());
        ahctCase.setSurvey_teacher(ahctCaseCode.getSurvey_teacher());
        ahctCase.setLoss_items(ahctCaseCode.getLoss_items());
        ahctCase.setDescribe(ahctCaseCode.getDescribe());
//        ahctCase.setDamage_customer_price(ahctCaseCode.getDamage_customer_price());
//        ahctCase.setWrite_off_price(ahctCaseCode.getWrite_off_price());
//        ahctCase.setCase_cost(ahctCaseCode.getCase_cost());
//        ahctCase.setOperation_cost(ahctCaseCode.getOperation_cost());
//        ahctCase.setGross_profit(ahctCaseCode.getGross_profit());
//        ahctCase.setReceipt_time(ahctCaseCode.getReceipt_time());
//        ahctCase.setPayment_stu(ahctCaseCode.getPayment_stu());
//        ahctCase.setDamage_customer(ahctCaseCode.getDamage_customer());
//        ahctCase.setRepair_payment(ahctCaseCode.getRepair_payment());
//        ahctCase.setRecipient_phone(ahctCaseCode.getRecipient_phone());
//        ahctCase.setInvoicing(ahctCaseCode.getInvoicing());
//        ahctCase.setBid_price(ahctCaseCode.getBid_price());
//        ahctCase.setIs_bidder(ahctCaseCode.getIs_bidder());
//        ahctCase.setIs_repair(ahctCaseCode.getIs_repair());

        ahctInformationMapper.insertLegalCase(ahctCase);
    }


    //案件管理信息修改
    @Override
    public void updateCase(AhctCaseCode ahctCaseCode) {

        List<String> list = ahctCaseCode.getAddress_code();
        String addResCode = String.join(",",list);
        AhctCase ahctCase = new AhctCase();
        ahctCase.setAddress_code(addResCode);
        ahctCase.setId(ahctCaseCode.getId());
        ahctCase.setCar_number(ahctCaseCode.getCar_number());
        ahctCase.setArea(ahctCaseCode.getArea());
        ahctCase.setCity(ahctCaseCode.getCity());
        ahctCase.setAccident_address(ahctCaseCode.getAccident_address());
        ahctCase.setCompany_name(ahctCaseCode.getCompany_name());
        ahctCase.setProvince(ahctCaseCode.getProvince());
        ahctCase.setReceiving_time(ahctCaseCode.getReceiving_time());
        ahctCase.setSurveyer(ahctCaseCode.getSurveyer());
        ahctCase.setCase_number(ahctCaseCode.getCase_number());
        ahctCase.setCase_type(ahctCaseCode.getCase_type());
        ahctCase.setDescribe(ahctCaseCode.getDescribe());
        ahctCase.setSurvey_teacher(ahctCaseCode.getSurvey_teacher());
        ahctCase.setLoss_items(ahctCaseCode.getLoss_items());
//        ahctCase.setDamage_customer_price(ahctCaseCode.getDamage_customer_price());
//        ahctCase.setWrite_off_price(ahctCaseCode.getWrite_off_price());
//        ahctCase.setCase_cost(ahctCaseCode.getCase_cost());
//        ahctCase.setOperation_cost(ahctCaseCode.getOperation_cost());
//        ahctCase.setGross_profit(ahctCaseCode.getGross_profit());
//        ahctCase.setReceipt_time(ahctCaseCode.getReceipt_time());
//        ahctCase.setPayment_stu(ahctCaseCode.getPayment_stu());
//        ahctCase.setDamage_customer(ahctCaseCode.getDamage_customer());
//        ahctCase.setRepair_payment(ahctCaseCode.getRepair_payment());
//        ahctCase.setRecipient_phone(ahctCaseCode.getRecipient_phone());
//        ahctCase.setInvoicing(ahctCaseCode.getInvoicing());
//        ahctCase.setBid_price(ahctCaseCode.getBid_price());
//        ahctCase.setIs_bidder(ahctCaseCode.getIs_bidder());
//        ahctCase.setIs_repair(ahctCaseCode.getIs_repair());

        ahctInformationMapper.updateLegalCase(ahctCase);
    }


    //案件管理信息删除
    @Override
    public void deleteCase(AhctCase ahctCase) {
        ahctInformationMapper.DeleteLegalCase(ahctCase);
    }

    /**
     * 导入用户数据
     *
     * @param CaseList 案件数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Transactional
    @Override
    public String importDataCase(List<AhctCase> userList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AhctCase Case : userList)
        {
            try {
                AhctCase c = ahctInformationMapper.selectCaseNumber(Case.getCase_number());
                if (StringUtils.isNull(c)) {
                    BeanValidatorsOne.validateWithException(validator, Case);
                    successNum++;
                    ahctInformationMapper.insertLegalCase(Case);
                    successMsg.append("<br/>" + successNum + "、信息 " + Case.getCase_number() + " 导入成功");
                } else {
                    failureNum++;
                    failureMsg.append("案件号 " + Case.getCase_number() + " 已存在");
                }
            }catch (Exception e){
                failureNum++;
                String msg = "案件号:" + Case.getCase_number() +" "+ "导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<AhctCase> selectCaseList(AhctCase ahctCase) {
        return ahctInformationMapper.selectSurveyGuery(ahctCase);
    }
}
